package com.aaa.home.service.Impl;

import com.aaa.common.entity.home.TbHeadNav;
import com.aaa.common.entity.home.TbLink;
import com.aaa.common.entity.home.TblZone;
import com.aaa.common.vo.Result;
import com.aaa.home.dao.TbLinkDao;
import com.aaa.home.service.TbLinkService;
import com.aaa.home.vo.AddLinkVo;
import com.aaa.home.vo.LinkVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

/**
 * @author ：Mickey
 * @date ：Created in 2022/12/8 0:57
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class TbLinkServiveImpl implements TbLinkService {

    @Autowired
    private TbLinkDao linkDao;

    @Autowired
    private TbLinkService linkService;

    @Override
    public Result getAllLinkPage(LinkVo linkVo, Integer current, Integer pageSize) {
        IPage<TbLink> page = new Page<>(current, pageSize);
        QueryWrapper<TbLink> wrapper = new QueryWrapper<>();
        if(StringUtils.hasText(linkVo.getLinkName())){

            wrapper.like("link_name",linkVo.getLinkName());
        }
        if (linkVo.getIsDisable()!=null){
            wrapper.eq("is_disable",linkVo.getIsDisable());
        }
        IPage<TbLink> page1 = linkDao.selectPage(page,wrapper);
        return new Result(2000,"分页查询友情链接成功",page1);
    }

    @Override
    public Result updateZoneStatus(String linkId, Integer isDisable) {
        TbLink tbLink = new TbLink(linkId, isDisable);
        int i = linkDao.updateById(tbLink);
        return new Result(2000,"修改链接状态成功",null);
    }

    @Override
    public Result addLink(AddLinkVo addLinkVo) {
        System.out.println(addLinkVo+"aaaaaaaaaaaaaaaaaaaaaaaaa");
        String linkName = addLinkVo.getLinkName();
        String linkUrl = addLinkVo.getLinkUrl();
        QueryWrapper<TbLink> wrapper = new QueryWrapper<>();
        wrapper.eq("link_name",linkName);
        TbLink selectHeadNav = linkDao.selectOne(wrapper);
        if(selectHeadNav!=null){
            linkService.deleteLinkById(selectHeadNav.getLinkId());
        }
        TbLink tbLink = new TbLink(linkName,linkUrl,0,0, LocalDateTime.now(),LocalDateTime.now());
        int insert = linkDao.insert(tbLink);
        return new Result(2000,"添加/修改头部连接成功",null);
    }

    @Override
    public Result deleteLinkById(String linkId) {
        int i = linkDao.deleteById(linkId);
        return new Result(2000,"删除友情链接成功",null);
    }
}
